<?php

// Define a 32-byte (64 character) hexadecimal encryption key
    // Note: The same encryption key used to encrypt the data must be used to decrypt the data
    //define('ENCRYPTION_KEY', 'd0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282');

Class EncryptDecrypt{
    // Encrypt Function
    public function mc_encrypt($encrypt){
        $ENCRYPTION_KEY='d0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282';
        $encrypt = serialize($encrypt);
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
        $key = pack('H*', $ENCRYPTION_KEY);
        $mac = hash_hmac('sha256', $encrypt, substr(bin2hex($ENCRYPTION_KEY), -32));
        $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $ENCRYPTION_KEY, $encrypt.$mac, MCRYPT_MODE_CBC, $iv);
        $encoded = base64_encode($passcrypt).'|'.base64_encode($iv);
        return $encoded;
    }
     
    // Decrypt Function
    public function mc_decrypt($decrypt){
        $ENCRYPTION_KEY='d0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282';
        $decrypt = explode('|', $decrypt);
        $decoded = base64_decode($decrypt[0]);
        $iv = base64_decode($decrypt[1]);
        if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; }
        $key = pack('H*', $ENCRYPTION_KEY);
        $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $ENCRYPTION_KEY, $decoded, MCRYPT_MODE_CBC, $iv));
        $mac = substr($decrypted, -64);
        $decrypted = substr($decrypted, 0, -64);
        $calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($ENCRYPTION_KEY), -32));
        if($calcmac!==$mac){ return false; }
        $decrypted = unserialize($decrypted);
        return $decrypted;
    }
}
?>